package com.mvp.shq.washer.ui.coupon;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.CouponAdapter;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.Coupon;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.mvp.shq.washer.view.other.AppBarStateChangeListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CouponActivity extends BaseActivity<CouponPresenter, CouponModel> implements CouponConstract.View {
    public static final int CHECK = 0;
    public static final int CHOOSE = 1;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.arl_not_data)
    AutoRelativeLayout arlNotData;
    @BindView(R.id.img_block)
    ImageView imgBlock;
    @BindView(R.id.tv_not_data)
    TextView tvNotData;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.collapsing_toolbar_layout)
    CollapsingToolbarLayout collapsingToolbarLayout;
    @BindView(R.id.appbar_layout)
    AppBarLayout appBarLayout;
    private List<Coupon> coupons;
    private CouponAdapter adapter;
    private boolean isLoadMore = false;
    private int pageNo = 1;
    private int pageSize = 10;
    private int TYPE;

    @Override
    public int getLayoutId() {
        return R.layout.activity_coupon;
    }

    @Override
    public void initView() {
        if (SpUtil.getUser() == null){
            JumpUtil.toLogin(context);
            finish();
            return;
        }
        tvNotData.setText("暂无优惠券");
        title.setText("优惠券");
        title.setVisibility(View.GONE);
        TYPE = this.getIntent().getExtras().getInt("type");
        imgBlock.setImageResource(R.mipmap.default_page_ticket);
        recyclerview.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.VERTICAL, 40, R.color.white);
        recyclerview.addItemDecoration(divider);
        coupons = new ArrayList<>();
        adapter = new CouponAdapter(context,coupons);
        recyclerview.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNo = 1;
                isLoadMore = false;
                refreshLayout.setEnableLoadmore(true);
                presenter.coupons(pageNo,pageSize);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageNo ++;
                isLoadMore = true;
                presenter.coupons(pageNo,pageSize);
            }
        });
        appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if( state == State.EXPANDED ) {

                    //展开状态
                    title.setVisibility(View.GONE);

                }else if(state == State.COLLAPSED){

                    //折叠状态
                    title.setVisibility(View.VISIBLE);

                }else {

                    //中间状态
                    title.setVisibility(View.GONE);

                }
            }
        });
        refreshLayout.autoRefresh();
    }

    private void hasData(){
        if (coupons != null && coupons.size() > 0){
            refreshLayout.setVisibility(View.VISIBLE);
            arlNotData.setVisibility(View.GONE);
        }else {
            refreshLayout.setVisibility(View.GONE);
            arlNotData.setVisibility(View.VISIBLE);
        }
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Subscribe(code = EventCode.ON_COUPON_CLICK)
    public void onCouponClick(Integer position){
        if (TYPE == CHECK){
            return;
        }
        if (!"0".equals(coupons.get(position).getShowStatus())){
            T.show("优惠券不可用");
            return;
        }
        RxBus.get().send(EventCode.CHOOSE_COUPON,coupons.get(position));
        finish();
    }

    @Override
    public void couponsS(List<Coupon> list) {
        if (list == null){
            hasData();
            return;
        }
        if (isLoadMore){
            refreshLayout.finishLoadmore();
        }else {
            coupons.clear();
            refreshLayout.finishRefresh();
        }
        coupons.addAll(list);
        hasData();
        adapter.notifyDataSetChanged();
        if (list.size() < pageSize){
            refreshLayout.setEnableLoadmore(false);
        }
    }

    @Override
    public void couponsF(String msg) {
        T.show(msg);
        if (isLoadMore){
            refreshLayout.finishLoadmore();
        }else {
            refreshLayout.finishRefresh();
        }
        hasData();
    }
}
